package com.ls.markt.controller.superLeague;

import com.ls.markt.dto.ClubDTO;
import com.ls.markt.enums.EventsEnum;
import com.ls.markt.model.admin.Club;
import com.ls.markt.service.admin.ClubService;
import com.ls.markt.service.admin.EventsService;
import com.ls.markt.service.admin.PlayerService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 概述 控制器类
 */
@RestController
@RequestMapping("/overview")
public class OverviewController {

    @Resource
    private ClubService clubService;

    @Resource
    private PlayerService playerService;

    @Resource
    EventsService eventsService;

    @GetMapping
    public ModelAndView index(Model model){
        Long eventsId  = eventsService.getIdByName(EventsEnum.SUPER.getCode());
        List<Club> clubList = clubService.listClubsBylevel(eventsId);
        List<ClubDTO> clubDTOS = new ArrayList<>();
        for (Club club:clubList){
            Map<String,Object> map = playerService.listInfoByClub(club.getId());
            ClubDTO clubDTO = new ClubDTO();
            clubDTO.setId(club.getId());
            clubDTO.setName(club.getName());
            clubDTO.setSquad((Long) map.get("count"));
            clubDTO.setAge(String.valueOf(map.get("age")));
            clubDTO.setValue(String.valueOf(map.get("value")));
            clubDTO.setTotalValue(String.valueOf(map.get("totalvalue")));
            clubDTOS.add(clubDTO);
        }
        model.addAttribute("clubList", clubDTOS);
        return new ModelAndView("superLeague/overview","superModel",model);
    }

}
